package cn.ligoo.dtu.web.springmvc.platform;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import cn.ligoo.dtu.domain.BmuMasterInfo;
import cn.ligoo.dtu.domain.UserInfo;
import cn.ligoo.dtu.service.impl.FacadeImpl;
import cn.ligoo.dtu.web.Constants;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/platform/bmu-master-info")
public class BmuMasterInfoController {
	private final Logger logger = LoggerFactory.getLogger(BmuMasterInfoController.class);

	@Resource
	private FacadeImpl facade;

	public BmuMasterInfoController() {
		logger.debug("...constructor");
	}

	@RequestMapping(value = "/search", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String search(HttpServletRequest request, @RequestParam int page, @RequestParam int rows, BmuMasterInfo t) {
		logger.debug("...search");

		logger.debug("...page:{}, rows:{}", page, rows);
		logger.debug("...bmuMaster:{}", t.toString());

		int total = facade.getBmuMasterInfoService().getBmuMasterInfoCount(t);

		t.getRow().setOffset((page - 1) * rows);
		t.getRow().setLimit(rows);
		UserInfo userInfo = (UserInfo) WebUtils.getSessionAttribute(request, Constants.SESSION_USER_INFO);
		if (null != userInfo && null != userInfo.getIs_admin() && 1 != userInfo.getIs_admin()) {
			t.getMap().put("user_id", userInfo.getId()); // 如果不是管理员，传入user_id，取得此user_id对应的所有modem_id
		}
		List<BmuMasterInfo> bmuMasterList = facade.getBmuMasterInfoService().getBmuMasterInfoList(t);

		String json = String.format(Constants.JSON_DATAGRID, JSON.toJSONString(bmuMasterList), total);
		logger.debug("...json:{}", json);
		return json;
	}
}
